<?php
    class admin_model_member extends model
    {
        public $group_id = '';
        public $user_id  = '';
        public $admin_group;

        public function __construct()
        {
            parent::__construct();
            $this->table_name = config::get()->table->admin;
            $this->admin_group = config::get()->table->admin_group;
        }

        /**
         * 验证用户是否存在
         * @param unknown_type $name
         * @param unknown_type $password
         */
        public function validate($name, $password)
        {
            $sql      = "select count(`id`) from `{$this->table_name}` where `name` = '{$name}' AND `password` = '{$password}'";

            if ($this->db->query($sql)->getCount()->query_result == 0)
            {
                return FALSE;
            }
            else
            {
                return TRUE;
            }
        }

        /**
         * 设置管理员ID
         * @param $id
         */
        public function set_user_id($id)
        {
            $this->user_id = $id;
        }

        /**
         * 保存用户，可用于新增和编辑.
         * @param unknown_type $name
         * @param unknown_type $password
         * @param unknown_type $group_id
         */
        public function save_user($name, $password, $group_id)
        {
            if ($this->user_id == '')
            {
                //新增
                $sql = "insert into `{$this->table_name}`(`name`, `password`, `group_id`, `status`) values('{$name}', '{$password}', '{$group_id}', 0)";
            }
            else
            {
                //编辑
                $sql = "UPDATE `{$this->table_name}` SET `name` = '{$name}', `password` = '{$password}', `group_id` = '{$group_id}' where `id` = {$this->user_id}";
            }

            $this->db->query($sql);

            if ($this->db->query_result)
            {
                return TRUE;
            }
            else
            {
                return FALSE;
            }
        }

        /**
         * 通过用户名获取信息
         * @param $name
         */
        public function through_username_get_information($name)
        {
            $sql = "select `id`, `name`, `group_id` from `{$this->table_name}` where `name` = '{$name}'";

            return $this->db->query($sql)->fetch_one()->query_result;
        }

        /**
         * 通过ID获取用户信息
         * @param unknown_type $id
         */
        public function through_userid_get_information($id)
        {
            $sql = "select `id`, `name`, `group_id` from `{$this->table_name}` where `id` = {$id}";

            return $this->db->query($sql)->fetch_one()->query_result;
        }

        /**
         * 获取管理员列表
         */
        public function get_user_list()
        {
            $sql = "select `id`, `name`, `group_id`, `status` from `{$this->table_name}`";

            return $this->db->query($sql)->fetch_all()->query_result;
        }

        /**
         * 获取管理组列表
         */
        public function get_user_group_list()
        {
            $sql = "select `id`, `name`, `permissions` from `{$this->admin_group}`";

            return $this->db->query($sql)->fetch_all()->query_result;
        }

        /**
         * 获取带有组的管理员列表
         */
        public function get_user_list_with_group()
        {
            $user_list  = $this->get_user_list();

            $group_list = $this->get_user_group_list();

            return $this->merge_user_group($user_list, $group_list);
        }

        /**
         * 合并用户列表与组列表
         * @param $user_list
         * @param $group_list
         */
        public function merge_user_group($user_list, $group_list)
        {
            foreach($user_list AS $k => $v)
            {
                foreach($group_list AS $k2 => $v2)
                {
                    if ($v['group_id'] == $v2['id'])
                    {
                        $user_list[$k][group_info] = $v2;
                    }
                }
            }

            return $user_list;
        }

        /**
         * 获取单个组信息
         * @param unknown_type $id
         */
        public function get_one_group($id)
        {
            $sql = "select * from `{$this->admin_group}` where id = {$id}";

            return $this->db->query($sql)->fetch_one()->query_result;
        }

        /**
         * 设置管理组ID，用来区分增加和编辑。
         * @param unknown_type $id
         */
        public function set_group_id($id)
        {
            $this->group_id = $id;
        }

        /**
         * 保存用户组信息，包括新增和编辑。
         * @param $name
         */
        public function save_group($name)
        {
            if ($this->group_id == '')
            {
                //新增
                $sql = "insert into `{$this->admin_group}`(`name`, `permissions`) values('{$name}', '')";
            }
            else
            {
                //编辑
                $sql = "update `{$this->admin_group}` SET `name`='{$name}' where `id` = {$this->group_id}";
            }

            $this->db->query($sql);

            if ($this->db->query_result)
            {
                return TRUE;
            }
            else
            {
                return FALSE;
            }
        }

        /**
         * 删除指定管理组
         * @param $id
         */
        public function delete_group($id)
        {
            $sql = "delete from `{$this->admin_group}` where `id` = {$id}";

            $this->db->query($sql);

            if ($this->db->query_result)
            {
                return TRUE;
            }
            else
            {
                return FALSE;
            }
        }

    	/**
         * 删除指定管理员
         * @param $id
         */
        public function delete_user($id)
        {
            $sql = "delete from `{$this->table_name}` where `id` = {$id}";

            $this->db->query($sql);

            if ($this->db->query_result)
            {
                return TRUE;
            }
            else
            {
                return FALSE;
            }
        }
    }